package com.le.tester.utils;

import cn.hutool.core.net.URLEncoder;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
 * createTime：2022/9/9 14:56
 * description：OSS上传工具
 */
public class OSSUtil {

    private static final Logger LOG = LoggerFactory.getLogger(OSSUtil.class);

    //@Value("${oss.accessKeyId}")
    private final static String accessKeyId = "LTAI4GFxz5Ue2pfsTdvbjPQV";

    //@Value("${oss.accessKeySecret}")
    private final static String accessKeySecret = "KgsvWvgbd9TFh3niE6BSv13ViP2fbq";

    // @Value("${oss.endpoint}")
    private final static String endpoint = "https://oss-cn-shenzhen.aliyuncs.com";

    // @Value("${oss.bucket}")
    private final static String bucket = "le-course";

    // @Value("${oss.domain}")
    private final static String ossDomain = "https://le-course.oss-cn-shenzhen.aliyuncs.com/";

    //文件流上传
    public static String uploadImgIS(String folderName, String fileName, String fileSuffix, InputStream ins) {
        String timestamp = String.valueOf(System.currentTimeMillis());
        String fileTotalName = folderName + "/" + fileName + "_" + timestamp + "." + fileSuffix;
        OSS ossClient = null;
        try {
            ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

            PutObjectResult result = ossClient.putObject(bucket, fileTotalName, ins);

            if (StringUtils.isBlank(result.getETag())) {
                return null;
            }
            return ossDomain + fileTotalName;
        } catch (Exception e) {
            LOG.info("上传异常：{}", e.getMessage());
            return null;
        } finally {
            if (Objects.nonNull(ossClient)) {
                ossClient.shutdown();
            }
        }
    }

    //网络流上传
    public static String uploadImgURLIS(String folderName, String fileName, String fileSuffix, String imgUrl) {
        String timestamp = String.valueOf(System.currentTimeMillis());
        String fileTotalName = folderName + "/" + fileName + "_" + timestamp + "." + fileSuffix;
        OSS ossClient = null;
        try {
            InputStream ins = new URL(imgUrl).openStream();
            ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

            PutObjectResult result = ossClient.putObject(bucket, fileTotalName, ins);

            if (StringUtils.isBlank(result.getETag())) {
                return null;
            }
            String filePath = ossDomain + fileTotalName;
            filePath = URLEncoder.createDefault().encode(filePath, StandardCharsets.UTF_8);
            System.out.println("file path is " + filePath);
            return filePath;
        } catch (Exception e) {
            LOG.info("上传异常：{}", String.valueOf(e));
            return null;
        } finally {
            if (Objects.nonNull(ossClient)) {
                ossClient.shutdown();
            }
        }
    }

}
